var ulit = require("../../utils/util.js")
var app = getApp();
Page({
    data: {
        containerShow: true,
        searchPanalShow: false,
        searchResult: {},
        searchinput: ""
    },
    onLoad: function(event) {
        var in_theatersUrl = app.globlData.doubanbase + "/v2/movie/in_theaters" + "?start=0&count=6";
        var coming_soonUrl = app.globlData.doubanbase + "/v2/movie/coming_soon" + "?start=0&count=6";
        var top250Url = app.globlData.doubanbase + "/v2/movie/top250" + "?start=0&count=6";
        this.getMovieData(in_theatersUrl, "正在热映", "in_theatersUrl");
        this.getMovieData(coming_soonUrl, "即将上映", "coming_soonUrl");
        this.getMovieData(top250Url, "top250", "top250Url");
    },
    getMovieData: function(url, title_in, setKey) {
        var that = this;
        wx.request({
            url: url,
            data: {},
            header: {
                'content-type': 'json' // 默认值
            },
            success: function(res) {
                that.in_theaters_fun(res.data, title_in, setKey)
            }
        })
    },
    onBindFocus: function(event) {
        this.setData({
            containerShow: false,
            searchPanalShow: true,
            searchinpu: event.detail.value,
        })
    },
    onBindImg: function() {
        this.setData({
            containerShow: true,
            searchPanalShow: false,
            searchinpu: "",
            searchResult: ""
        })
    },
    onBindConfirm: function(event) {
        this.data.searchinpu = event.detail.value
        // var text = event.detail.value;
        var searchUrl = app.globlData.doubanbase + "/v2/movie/search?q=" + this.data.searchinpu;
        this.getMovieData(searchUrl, "", "searchResult");
    },
    in_theaters_fun: function(data, title_in, setKey) {
        var movies = [];
        for (var id in data.subjects) {
            var subjects = data.subjects[id];
            var title = subjects.title;
            if (title.length >= 6) {
                title = title.substring(0, 6) + "...";
            }
            var temp = {
                stars: ulit.public_funtion(subjects.rating.stars),
                title: title,
                average: subjects.rating.average,
                images: subjects.images.large,
                id: subjects.id
            }
            movies.push(temp);
        }
        var readyData = {};
        readyData[setKey] = {
            movies: movies,
            title_in: title_in
        };
        this.setData(readyData);
    },
    more_movies: function(event) {
        var categorys = event.currentTarget.dataset.category;
        wx.navigateTo({
            url: 'more_movies/more_movies?categorys=' + categorys
        })
    },
    //进入详情页
    onDetail:function(event){
        console.log(event)
        var moviesId=event.currentTarget.dataset.id;
        wx.navigateTo({
            url: 'movies_detail/movies_detail?id=' + moviesId,
        })
    }
})